﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class _17 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        IMyEnumerable list = new MyList();
        IMyEnumerator myEnumerator = list.GetEnumerator();
        for (int i = 0; i < list.Count; i++)
        {
            object current = myEnumerator.Current;
            myEnumerator.MoveNext();
        }
        while (myEnumerator.MoveNext())
        {
            object current = myEnumerator.Current;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

public class MyEnumerator : IMyEnumerator
{
    public int index = 1;
    public MyList mylist;

    public MyEnumerator(MyList myList)
    {
        mylist = myList;
    }

    public object Current
    {
        get { return mylist[index - 1]; }
    }

    public bool MoveNext()
    {
        if (index + 1 > mylist.Count)
        {
            index = 1;
            return false;
        }
        else
        {
            index++;
            return true;
        }
    }
}

public class MyList : IMyEnumerable
{
    public object[] items = new object[10];
    public IMyEnumerator myEnumerator;

    public object this[int i]
    {
        get { return items[i]; }
        set { this.items[i] = value; }
    }

    public int Count
    {
        get { return items.Length; }
    }

    public IMyEnumerator GetEnumerator()
    {
        if (myEnumerator == null)
        {
            myEnumerator = new MyEnumerator(this);
        }
        return myEnumerator;
    }
}

public interface IMyEnumerator
{
    bool MoveNext();
    object Current { get; }
}

public interface IMyEnumerable
{
    IMyEnumerator GetEnumerator();
    int Count { get; }
}
